/**
 * Copyright (C) 2007 The AsyncMail Group <asyncmail@googlegroups.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.asyncmail.smtpserver.mock;

import com.googlecode.asyncmail.dnsservice.DNSService;
import com.googlecode.asyncmail.dnsservice.TemporaryResolutionException;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MockDNSService implements DNSService {
    private Map<String, List<String>> mxMap = new HashMap<String, List<String>>();

    private Map<String, List<String>> txtMap = new HashMap<String, List<String>>();

    private Map<String, List<String>> smtpAddressMap = new HashMap<String, List<String>>();

    private List<String> resolveableHosts = new ArrayList<String>();

    private String hostName = "localhost";

    private boolean temporaryResolutionException = false;

    public void setResolveableHost(List<String> resolveableHosts) {
        this.resolveableHosts = resolveableHosts;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public void addMXRecords(String hostname, List<String> records) {
        mxMap.put(hostname, records);
    }

    public void removeMXRecords(String hostname) {
        mxMap.remove(hostname);
    }

    public void addTXTRecords(String hostname, List<String> records) {
        txtMap.put(hostname, records);
    }

    public void removeTXTRecords(String hostname) {
        txtMap.remove(hostname);
    }

    public Collection<String> findMXRecords(String hostname)
            throws TemporaryResolutionException {
        if (temporaryResolutionException)
            throw new TemporaryResolutionException("TemporaryResultion problem");
        return mxMap.get(hostname);
    }

    public Collection<String> findTXTRecords(String hostname) {
        return txtMap.get(hostname);
    }

    public InetAddress[] getAllByName(String host) throws UnknownHostException {
        return new InetAddress[] { getByName(host) };
    }

    public InetAddress getByName(String host) throws UnknownHostException {
        if (resolveableHosts.contains(host)) {
            return getLocalHost();
        }
        throw new UnknownHostException("Unknown host");
    }

    public String getHostName(InetAddress addr) {
        return hostName;
    }

    public InetAddress getLocalHost() throws UnknownHostException {
        return InetAddress.getLocalHost();
    }

    public Iterator<String> getSMTPHostAddresses(String domainName)
            throws TemporaryResolutionException {
        if (temporaryResolutionException)
            throw new TemporaryResolutionException("TemporaryResultion problem");
        return smtpAddressMap.get(domainName).iterator();
    }

}
